<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-refresh-right" size="mini" @click="getList">刷新</el-button>
      </el-col>

    </el-row>

    <el-table v-loading="loading" :data="list">
      <el-table-column label="入库时间" align="center" prop="InputTime" />

      <el-table-column label="车牌号" align="center" prop="CarNo" />
      <el-table-column label="入库件数" align="center" prop="InputCout" />
      <el-table-column label="入库吨数" align="center" prop="InputTon" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-delete" @click="changeItem(scope.row)">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>


    <!-- 添加或修改推荐re对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form :model="form" label-width="80px">
        <el-form-item label="入库时间">
          <el-date-picker v-model="inputDate" type="date" placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="货代公司">
          <el-button type="primary" @click="openItem = true">选择</el-button>
          <span style="margin:10px">{{ showCName(form.CompanyId) }} </span>
        </el-form-item>
        <el-form-item label="车牌号">
          <el-input v-model="form.CarNo" placeholder="车牌号" />
        </el-form-item>

        <el-form-item label="入库件数">
          <el-input v-model="form.InputCout" placeholder="入库件数" />

        </el-form-item>
        <el-form-item label="入库吨数">
          <el-input v-model="form.InputTon" placeholder="入库吨数" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>

    </el-dialog>


    <el-dialog title="选择货代公司" :visible.sync="openItem" width="80%" append-to-body>
      <CompanyCVue @companyC="getCInfo" v-if="openItem" />
    </el-dialog>
    <div>
      <span>
        入库总吨数{{ totalWight }}吨</span>
    </div>
  </div>
</template>

<script setup>


import {
  addInput,
  deleteInput,
  updateInput,
  inputList
} from '@/api/portMange';
import { DateToStr } from "@/utils/dateUtils"
import CompanyCVue from '../components/Company-C.vue';
import { getCompany } from '@/api/company.js';
import { ref, reactive } from 'vue';

let list = reactive([])
let clist = reactive([])
let loading = ref(true)
let title = ref("")
let openItem = ref(false)
let open = ref(false)
let form = reactive({})
let inputDate = ref('')
let totalWight = ref(0)



// 修改
function changeItem(row) {
  open = true;
  form = row;
  if (row.InputTime) {
    inputDate = new Date(row.InputTime)

  }

}
// 获取列表
function getList() {
  loading = false;
  inputList({}).then(res => {
    list = res.data.value
    console.log(list, "o");
    totalWight = getTotol(list, "InputTon")
    loading = false;
  })
  getCompany({}).then(res => {
    clist = res.data.value
  })

}
function getTotol(arr = [], key) {
  let tem = 0;
  arr.forEach(item => {
    tem = tem + parseInt(item[key])
  })
  return tem;
}
// 取消按钮
function cancel() {
  open = false;

}


/** 新增按钮操作 */
function handleAdd() {
  form = {}
  open = true;
  title = "添加";
}

/** 提交按钮 */
function submitForm() {
  form.InputTime = DateToStr(inputDate)
  if (form.Id) {
    updateInput(form).then(res => {
      cancel()
      getList();

    })
  } else {
    addInput(form).then(res => {
      cancel()
      getList();
    })
  }
}
/** 删除按钮操作 */
function handleDelete(row) {
  let that = this;
  that.$confirm('确定删除?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    deleteInput({
      ids: row.Id
    }).then(res => {
      that.getList();
    })
  }).catch(() => { })
}
function getCInfo(data) {
  openItem = false;
  form.CompanyId = data[0].Id
}

function showCName(id) {
  let dat = "";
  if (id) {
    for (let i = 0; i < clist.length; i++) {
      console.log(clist[i], "---");
      if (clist[i].Id == id) {
        dat = clist[i].Name

      }
    }
  }
  return dat;
}
  }


</script>
